#!/usr/bin/php
<?php
	
	$dirs = array(
		'/home/vladimir/public_html/www.embe.lo/application/templates/',
		'/home/vladimir/public_html/www.embe.lo/application/templates/control/',
		'/home/vladimir/public_html/www.embe.lo/application/templates/control/helpers/',
		'/home/vladimir/public_html/www.embe.lo/application/templates/default/',
		'/home/vladimir/public_html/www.embe.lo/application/templates/default/helpers/',
		'/home/vladimir/public_html/www.embe.lo/application/configs/forms/control/',
		'/home/vladimir/public_html/www.embe.lo/application/configs/grids/control/',
		'/home/vladimir/public_html/www.embe.lo/application/configs/forms/default/',
		'/home/vladimir/public_html/www.embe.lo/application/configs/grids/default/',
		'/home/vladimir/public_html/www.embe.lo/application/configs/menus/control/',
		'/home/vladimir/public_html/www.embe.lo/application/configs/menus/default/',
		'/home/vladimir/public_html/www.embe.lo/library/Mbe/Controller/',
		'/home/vladimir/public_html/www.embe.lo/library/Mbe/Controller/Module/Control/',
		'/home/vladimir/public_html/www.embe.lo/library/Mbe/Controller/Module/Default/',
		
		'/home/vladimir/public_html/test.mordo.ru/application/templates/',
		'/home/vladimir/public_html/test.mordo.ru/application/templates/control/',
		'/home/vladimir/public_html/test.mordo.ru/application/templates/control/helpers/',
		'/home/vladimir/public_html/test.mordo.ru/application/templates/default/',
		'/home/vladimir/public_html/test.mordo.ru/application/templates/default/helpers/',
		'/home/vladimir/public_html/test.mordo.ru/application/configs/forms/control/',
		'/home/vladimir/public_html/test.mordo.ru/application/configs/grids/control/',
		'/home/vladimir/public_html/test.mordo.ru/application/configs/forms/default/',
		'/home/vladimir/public_html/test.mordo.ru/application/configs/grids/default/',
		'/home/vladimir/public_html/test.mordo.ru/application/configs/menus/control/',
		'/home/vladimir/public_html/test.mordo.ru/application/configs/menus/default/',
		'/home/vladimir/public_html/test.mordo.ru/library/Phorm/Controller/',
		'/home/vladimir/public_html/test.mordo.ru/library/Phorm/Controller/Module/Control/',
		'/home/vladimir/public_html/test.mordo.ru/library/Phorm/Controller/Module/Default/',
	);
	
	set_include_path(implode(PATH_SEPARATOR, array(
		realpath('../../library/'),
		get_include_path(),
	)));
	
	require_once('Zend/Db.php');
	require_once('Zend/Config.php');
	require_once('Zend/Config/Xml.php');
	
	$cfg = new Zend_Config_Xml('../configs/application/www.iship.xml','production');
	//print_r($cfg);
	$db = Zend_Db::factory($cfg->resources->db->adapter,$cfg->resources->db->params);
	
	$out = array(
		'Данные успешно сохранены',
		'При заполнении формы были обнаружены ошибки',
		'<span style="color:green">Да</span>',
		'<span style="color:green">Нет</span>',
		'<span style="color:green">Действующий</span>',
		'<span style="color:red">Заблокирован</span>',
	);
	
	foreach($dirs as $dir) {
		if ($handle = @opendir($dir)) {
			while (false !== ($entry = readdir($handle))) {
				$extension = pathinfo($entry, PATHINFO_EXTENSION);
				switch ($extension) {
				
					case 'xml':
						
						$str = file_get_contents($dir . $entry);
						
						if(preg_match_all('#<label>([^<]+)</label>#s',$str,$found)) {
							$out = array_merge($out,$found[1]);
						}
						if(preg_match_all('#<display>([^<]+)</display>#s',$str,$found)) {
							$out = array_merge($out,$found[1]);
						}
						if(preg_match_all('#<title>([^<]+)</title>#s',$str,$found)) {
							$out = array_merge($out,$found[1]);
						}
						if(preg_match_all('#<legend>([^<]+)</legend>#s',$str,$found)) {
							$out = array_merge($out,$found[1]);
						}
						if(preg_match_all('#<description>([^<]+)</description>#s',$str,$found)) {
							$out = array_merge($out,$found[1]);
						}
						if(preg_match_all('#<placeholder>([^<]+)</placeholder>#s',$str,$found)) {
							$out = array_merge($out,$found[1]);
						}
						if(preg_match_all('#<replace>([^<]+)</replace>#s',$str,$found)) {
							$out = array_merge($out,$found[1]);
						}
						if(preg_match_all('#<multioptions>(.*?)</multioptions>#s',$str,$found)) {
							foreach($found[1] as $cmp) {
								if(preg_match_all('#</key>\s*<value>([^<]+)</value>#s',$cmp,$opts)) {
									$out = array_merge($out,$opts[1]);
								} elseif(preg_match_all('#<[^>]+>([^<]+)#s',$cmp,$opts)) {
									$out = array_merge($out,$opts[1]);
								}
							}
						}
						/*if(preg_match_all('#_\([\'"]{1}([^\'"]+)#',$str,$found)) {
							$out = array_merge($out,$found[1]);
						}*/
						if(preg_match_all('#<description>\s*<\!\[CDATA\[([^\]]+)\]\]></description>#s',$str,$found)) {
							$out = array_merge($out,$found[1]);
						}
						
						break;
						
					case 'php':
						
						$str = file_get_contents($dir . $entry);
						
						if(preg_match_all('#_\([\'"]{1}([^\'"]+)#',$str,$found)) {
							$out = array_merge($out,$found[1]);
						} 
						if(preg_match_all('#composeErrorMessage\s*\([\'"]{1}([^\'"]+)#',$str,$found)) {
							$out = array_merge($out,$found[1]);
						} 
						if(preg_match_all('#composeSuccessMessage\s*\([\'"]{1}([^\'"]+)#',$str,$found)) {
							$out = array_merge($out,$found[1]);
						}
						
						break;
						
					case 'tpl':
						
						$str = file_get_contents($dir . $entry);
						
						if(preg_match_all('#\{\$translate->_\([\'"]{1}([^\'"]+)#',$str,$found)) {
							$out = array_merge($out,$found[1]);
						}
						
						break;
				
				}
			}
		}
	}
	
	/**
	 * Выборка из базы имен ролей, для ЕМБЕ статусы, документы
	 */
	 $out = array_merge($out,$db->fetchCol('SELECT rolename FROM pw_roles'));
	 $out = array_merge($out,$db->fetchCol('SELECT resourcename FROM pw_mvc_resources'));
	 $out = array_merge($out,$db->fetchCol('SELECT statusname FROM pw_statuses'));
	 $out = array_merge($out,$db->fetchCol('SELECT attachmentname FROM pw_attachments'));
	
	foreach ($out as $k=>$v) {
		if(trim($v) == '') {
			unset($out[$k]);
		} else {
			$out[$k] = '_("' . trim(addslashes($v)) . '");';
		}
	}
	
	$out = array_unique($out);
	sort($out);
	
	file_put_contents('./tmp.php','<?php' . PHP_EOL . implode(PHP_EOL,$out) . PHP_EOL);

	
